<?
require_once(dirname(__FILE__) . '/../../classes/report_interface.inc.php');

class LoggerModuleSourcesMainReport implements LoggerReportInterface
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }
    

    public function GetLinksData($date1, $date2)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';

        $SelectSQL = 'SELECT SUM(`summ`) AS `summ` FROM (';

        $SelectSQL .= 'SELECT SUM(`s`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_sources_links_archive` `s` ';
        $SelectSQL .= 'WHERE `s`.`date`>='.$date1.' AND `s`.`date`<='.$date2.' ';
        $SelectSQL .= 'UNION ALL ';
        $SelectSQL .= 'SELECT COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_sources_links` `s` ';
        $SelectSQL .= 'WHERE `s`.`date`>='.$date1.' AND `s`.`date`<='.$date2.' ';
        
        $SelectSQL .= ') AS `__temp_query` ';

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
        if ($row = $this->database->FetchAssoc($result))
        {
            return $row['summ'];
        }

        return 0;
    }

    public function GetSearchData($date1, $date2)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';

        $SelectSQL = 'SELECT SUM(`summ`) AS `summ` FROM (';

        $SelectSQL .= 'SELECT SUM(`s`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_sources_search_archive` `s` ';
        $SelectSQL .= 'WHERE `s`.`date`>='.$date1.' AND `s`.`date`<='.$date2.' ';
        $SelectSQL .= 'UNION ALL ';
        $SelectSQL .= 'SELECT COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_sources_search` `s` ';
        $SelectSQL .= 'WHERE `s`.`date`>='.$date1.' AND `s`.`date`<='.$date2.' ';
        
        $SelectSQL .= ') AS `__temp_query` ';

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
        if ($row = $this->database->FetchAssoc($result))
        {
            return $row['summ'];
        }

        return 0;
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $xml = $root->ownerDocument;

        $name = $xml->createElement('report_name', 'Источники посещений');
        $root->appendChild($name);

        $linksData = $this->GetLinksData($date1, $date2);
        $searchData = $this->GetSearchData($date1, $date2);
        $reportData = $xml->createElement('report_data');
        $root->appendChild($reportData);
        $reportData->setAttribute('servers', intval($linksData));
        $reportData->setAttribute('engines', intval($searchData));

        $xsl = dirname(__FILE__) . '/templates/main.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}
